package service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.CategoryDao;
import dao.UsersDao;
import entity.Category;
import service.CategoryService;

@Service
public class CategoryServiceImpl implements CategoryService {

	// 注解的方式注入SqlSessionFactory
	@Autowired
	SqlSessionFactory factory;

	@Override
	public List<Category> find() {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession();
			CategoryDao dao = sqlSession.getMapper(CategoryDao.class); // 动态代理生成对象
			List<Category> category = dao.find();
			return category;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return null;
	}

	@Override
	public Category findById(int id) {
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession();
			CategoryDao dao = sqlSession.getMapper(CategoryDao.class); // 动态代理生成对象
			Category category = dao.findById(id);
			return category;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			sqlSession.close();
		}
		return null;
	}

}
